import 'dart:ffi';

import 'package:battery_plus/battery_plus.dart';
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter_device_info_plugin/flutter_device_info_plugin.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _idfa = 'Unknown';
  String _idfv = 'Unknown';
  String _appVersion = 'Unknown';
  String _sysVersion = 'Unknown';
  String _sysName = 'Unknown';
  String _originalDeviceInfo = 'Unknown';
  String _deviceNameplate = 'Unknown';
  bool _isSimulator = false;
  bool _isPhoneJailbreak = false;

  String _totalDiskSize = 'Unknown';
  String _totalMemorySize = 'Unknown';
  String _availableDiskSize = 'Unknown';
  String _availableMemorySize = 'Unknown';

  String _batteryLevel = 'Unknown';
  bool _batteryState = false;

  String _timeZone = 'Unknown';
  String _languageCode = 'Unknown';

  String _netReachableType = 'Unknown';
  
  Map<dynamic,dynamic> _wifiInfo = {};

  final _flutterDeviceInfoPlugin = FlutterDeviceInfoPlugin();

  @override
  void initState() {
    super.initState();
    initPlatformState();

    initBattery();
  }

  initBattery() async {
    final Battery battery = Battery();
    final level = await battery.batteryLevel;

    battery.onBatteryStateChanged.listen((BatteryState state) {
      setState(() {
        _batteryState = state == BatteryState.charging;
      });
    });

    setState(() {
      _batteryLevel = level.toString();
    });
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String idfa;
    String idfv;
    String appVersion;
    String sysVersion;
    String sysName;
    String originalDeviceInfo;
    String deviceNameplate;
    bool isSimulator;
    bool isPhoneJailbreak;

    String totalDiskSize;
    String totalMemorySize;
    String availableDiskSize;
    String availableMemorySize;

    String timeZone;
    String languageCode;

    String netReachableType;

    Map<dynamic,dynamic> wifiInfo;

    try {
      idfa =
          await _flutterDeviceInfoPlugin.getIDFA() ?? '';
      idfv = await _flutterDeviceInfoPlugin.getIDFV() ?? '';
      appVersion = await _flutterDeviceInfoPlugin.getAppVersion() ?? '';
      sysVersion = await _flutterDeviceInfoPlugin.getSystemVersion() ?? '';
      sysName = await _flutterDeviceInfoPlugin.getSystemName() ?? '';
      originalDeviceInfo = await _flutterDeviceInfoPlugin.originalDeviceInfo() ?? '';
      deviceNameplate = await _flutterDeviceInfoPlugin.deviceNameplate() ?? '';
      isSimulator = await _flutterDeviceInfoPlugin.isSimulator() ?? false;
      isPhoneJailbreak = await _flutterDeviceInfoPlugin.isPhoneJailbreak() ?? false;

      totalDiskSize = await _flutterDeviceInfoPlugin.getTotalDiskSize() ?? '';
      totalMemorySize = await _flutterDeviceInfoPlugin.getTotalMemorySize() ?? '';
      availableDiskSize = await _flutterDeviceInfoPlugin.getAvailableDiskSize() ?? '';
      availableMemorySize = await _flutterDeviceInfoPlugin.getAvailableMemorySize() ?? '';

      timeZone = await _flutterDeviceInfoPlugin.getLocalTimeZone() ?? '';
      languageCode = await _flutterDeviceInfoPlugin.getLocalLanguageCode() ?? '';

      netReachableType = await _flutterDeviceInfoPlugin.getNetReachableType() ?? '';

      wifiInfo = await _flutterDeviceInfoPlugin.getWifiInfo() ?? {};

      _flutterDeviceInfoPlugin.reportFaceBook("1", "2", "3", "4");

    } on PlatformException {
      idfa = 'Failed to get platform version.';
      idfv = 'Failed to get platform version.';
      appVersion = 'Failed to get platform version.';
      sysVersion = 'Failed to get platform version.';
      sysName = 'Failed to get platform version.';
      originalDeviceInfo = 'Failed to get platform version.';
      deviceNameplate = 'Failed to get platform version.';
      isSimulator = false;
      isPhoneJailbreak = false;

      totalDiskSize = 'Failed to get platform version.';
      totalMemorySize = 'Failed to get platform version.';
      availableDiskSize = 'Failed to get platform version.';
      availableMemorySize = 'Failed to get platform version.';

      timeZone = 'Failed to get platform version.';
      languageCode = 'Failed to get platform version.';
      netReachableType = 'Failed to get platform version.';
      
      wifiInfo = {};
    }

    if (!mounted) return;

    setState(() {
      _idfa = idfa;
      _idfv = idfv;
      _appVersion = appVersion;
      _sysVersion = sysVersion;
      _sysName = sysName;
      _originalDeviceInfo = originalDeviceInfo;
      _deviceNameplate = deviceNameplate;
      _isSimulator = isSimulator;
      _isPhoneJailbreak = isPhoneJailbreak;

      _totalDiskSize = totalDiskSize;
      _totalMemorySize = totalMemorySize;
      _availableDiskSize = availableDiskSize;
      _availableMemorySize = availableMemorySize;

      _timeZone = timeZone;
      _languageCode = languageCode;

      _netReachableType = netReachableType;
      
      _wifiInfo = wifiInfo;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: SingleChildScrollView(
          child: Column(
            children: [
              Text('IDFA: $_idfa\n'),
              Text('IDFV: $_idfv\n'),
              Text('AppVersion: $_appVersion\n'),
              Text('SystemVersion: $_sysVersion\n'),
              Text('SystemName: $_sysName\n'),
              Text('原始设备: $_originalDeviceInfo\n'),
              Text('设备铭牌: $_deviceNameplate\n'),
              Text('是否是模拟器: ${_isSimulator ? "是" : "否"}\n'),
              Text('是否越狱: ${_isPhoneJailbreak ? "是" : "否"}\n'),

              const Text('=====================================\n'),

              Text('总磁盘容量: $_totalDiskSize\n'),
              Text('总内存大小: $_totalMemorySize\n'),
              Text('可用磁盘容量: $_availableDiskSize\n'),
              Text('可用内存大小: $_availableMemorySize\n'),

              const Text('=====================================\n'),

              Text('电池电量: $_batteryLevel\n'),
              Text('是否正在充电: ${_batteryState ? "是" : "否"}\n'),

              const Text('=====================================\n'),

              Text('时区ID: $_timeZone\n'),
              Text('语言: $_languageCode\n'),
              Text('网络类型: $_netReachableType\n'),

              const Text('=====================================\n'),
              
              Text('Wifi信息：$_wifiInfo\n'),

              TextButton(onPressed: (){
                FlutterDeviceInfoPlugin().openContact();
              }, child: const Text("点击通讯录")),

              const SizedBox(height: 20),
            ],
          ),
        ),
      ),
    );
  }
}
